<?
include_once('config.php');
class DbBridge {
    var $connection;
    
    var $errorLog = array();
    
    var $requests = array();
   

    function __construct () {
        $this->connection = NULL;
        $this->errorLog  = array();
        $this->requests   = array();
    }

    public function connect () {
        $this->connection = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_BASENAME);
        if (!$this->connection) {
        	$this->pushError(mysqli_connect_error());
        	return false;
        }
        if (!mysqli_query($this->connection, "SET NAMES 'utf8'")) {
            $this->pushConnectionError();
        	return false;
        }
        return true;
    }
    public function disconnect () {
        if ($this->connection) {
            $this->connection->close();
        }
        return false;
    }
    public function query ($query) {
        if (!$query) {
            $this->pushError("DbBridge::query parameter 'query' is not set");
            return false;
        }
        if (!is_string($query)) {
            $this->pushError("DbBridge::query parameter 'query' is not string");
            return false;
        }
    	$this->pushRequest($query);
    	$resp = $this->executeQuery($query);
        if (!$resp) {
            $this->pushConnectionError();
        	return false;
        }
        return $resp;
    }
    public function queryEx ($source_tpl, $args_a, $args_b) { 
        $online = array();
        $keys   = array();
        for ($i = 0; $i < count($args_b); $i++){
            $keys[$i] = $this->connection->real_escape_string($args_b[$i]);
        }
        $online = array_combine($args_a, $keys);
        $query = strtr($source_tpl, $online);
        $this->pushRequest($query);
        $resp = $this->executeQuery($query);
        if (!$resp) {
            $this->pushConnectionError();
        	return false;
        }
        return $resp;
    }
    public function getRows ($result) {
        if (!$result) {
            $this->pushError("DbBridge::getRows parameter '$result' is not defined");
            return false;
        }
        if (!is_object($result)) {
            $this->pushError("DbBridge::getRows parameter '$result' is not object");
            return false;
        }
        if (get_class($result) != 'mysqli_result') {
            $this->pushError("DbBridge::getRows parameter '$result' has not mysqli_result class");
            return false;
        }
        for ($rows = array(); $row = $result->fetch_assoc();)
            $rows[] = $row;
        if (!$rows || count($rows) == 0) {
            echo $rows->length;
            $this->pushConnectionError();
            $this->pushError($result->error);
            return false;
        }
        return $rows;
    }
    public function getCount ($result) {
        if (!$result) {
            $this->pushError("DbBridge::getCount parameter '$result' is not defined");
            return false;
        }
        if (!is_object($result)) {
            $this->pushError("DbBridge::getCount parameter '$result' is not object");
            return false;
        }
        if (get_class($result) != 'mysqli_result') {
            $this->pushError("DbBridge::getCount parameter '$result' has not mysqli_result class");
            return false;
        }
        $temp = mysqli_num_rows($result);
        if (!$temp) {
            $this->pushConnectionError();
        	return false;
        }
        return intval($temp);
    }
    public function getErrors () { return $this->errorLog; }

    public function getRequests () { return $this->requests; }

    public function getLastRequest () { return $this->requests[count($this->requests) - 1]; }


    private function pushConnectionError() { $this->pushError($this->connection->error); }

    private function pushError ($error) { array_push($this->errorLog, $error); }

    private function pushRequest ($request) { array_push($this->requests, $request); }

    private function executeQuery ($query) { return $this->connection->query($query); }
}
?>